package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2025-04-28
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {



    @Override
    public List<CategoryBrand> selectCategoryBrandList() {

        return baseMapper.selectCategoryBrandList();
    }

    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        //根据品牌ID+分类ID查询是否存在 存在：抛出异常
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(CategoryBrand::getCategoryId,categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId,categoryBrand.getBrandId());
        long count = count(queryWrapper);
        if (count>0){
            throw new SecurityException("此类已有该品牌");
        }
        //保存分类品牌关系
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return baseMapper.insert(categoryBrand);
    }

    @Override
    public String selectBrandListByCategoryId(Long categoryId) {
        return baseMapper.selectBrandListByCategoryId(categoryId);
    }
}
